// pages/featuredvideos/featuredvideos.js
const api = require('../../util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        videolists: [],
        page: 1,
        pagtotal: 0,
        finderusername: ''
    },
    getVideoId() {
        var that = this
        api.http('/common/getVideoId', 'POST', false, {
        }, function (res) {
          if(res.code == 200) {
            that.setData({
                finderusername: res.data.finder_user_name
            })
          }
        })
    },
    // 点击观看
    channelfun() {
        var that = this
        api.http('/UserIntegral/AddIntegral', 'POST', false, {
            type: 4
        }, function (res) {
        })
    },
    // 热门数据
    featuredVideos(e) {
        var that = this,type= false
        if (typeof(e)!='undefined') {
            type = true
        } 
        api.http('/index/featuredVideos', 'POST', type, {
            page: this.data.page,
            limit: 20
        }, function (res) {
            if(res.code == 200) {
                if (that.data.page == 1) {
                    that.setData({
                        videolists: res.data.data,
                        pagtotal: res.data.total
                    })
                } else {
                    that.setData({
                        videolists: that.data.videolists.concat(res.data.data),
                        pagtotal: res.data.total
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.featuredVideos()
        this.getVideoId()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        // 上拉加载更多loading
        let _length = this.data.videolists.length
        var that = this
        // 列表长度与列表总数对比
        if (_length != this.data.pagtotal) {
            // 当前页码加一
            this.setData({
                page: that.data.page + 1
            })
            // 更新数据
            this.featuredVideos('123')
        } else {
            wx.showToast({
                title: '没有更多了~',
                icon: 'none',
                duration: 1000
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    // 分享到朋友圈
    onShareTimeline(){
        return {
          title: '精选视频',
        // query: 'id=' + this.id,
        // imageUrl: ''
        }
    }
})